{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-02-23T02:32:04.614327Z",
     "start_time": "2024-02-23T02:32:04.608951Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张三,同事,13511112222\\n', '李四,朋友,13900001111\\n', '王武,同事,18912344321']\n"
     ]
    }
   ],
   "source": [
    "# 实现手机通讯录\n",
    "with open(\"./24-demo.csv\") as f:\n",
    "    file_data = f.readlines()\n",
    "    print(file_data)"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 安装软件包并导入当前代码\n",
    "# ! pip3 install tinydb\n",
    "from tinydb import TinyDB"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-23T02:35:05.262005Z",
     "start_time": "2024-02-23T02:35:05.250393Z"
    }
   },
   "id": "f5fb7c13de0d835c",
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 指定存放通讯录的文件\n",
    "db = TinyDB(\"./db.json\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-23T02:35:16.800386Z",
     "start_time": "2024-02-23T02:35:16.794774Z"
    }
   },
   "id": "4c762861e7be7c1b",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "'\\n问题\\n1 假如文件中有更多的行怎样简便处理？\\n2 怎样知道已经处理完所有的行了？\\n3 如果文件格式不规范怎么办？\\n4 这么写很啰嗦，有没有更优雅的写法？\\n'"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#将csv格式的文件，进行格式转换，并存入到通讯录文件\n",
    "friend_1 = {'name':file_data[0].split(',')[0], 'source': file_data[0].split(',')[1], 'tel': file_data[0].split(',')[2].strip()}\n",
    "friend_2 = {'name':file_data[1].split(',')[0], 'source': file_data[1].split(',')[1], 'tel': file_data[1].split(',')[2].strip()}\n",
    "friend_3 = {'name':file_data[2].split(',')[0], 'source': file_data[2].split(',')[1], 'tel': file_data[2].split(',')[2].strip()}\n",
    "\"\"\"\n",
    "问题\n",
    "1 假如文件中有更多的行怎样简便处理？\n",
    "2 怎样知道已经处理完所有的行了？\n",
    "3 如果文件格式不规范怎么办？\n",
    "4 这么写很啰嗦，有没有更优雅的写法？\n",
    "\"\"\""
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-23T02:40:55.618800Z",
     "start_time": "2024-02-23T02:40:55.616447Z"
    }
   },
   "id": "cf562905deb92ecb",
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "[1, 2, 3]"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将通讯录中的好友信息写入数据库\n",
    "db.insert_multiple([\n",
    "    friend_1,\n",
    "    friend_2,\n",
    "    friend_3\n",
    "])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-23T02:41:58.199021Z",
     "start_time": "2024-02-23T02:41:58.192444Z"
    }
   },
   "id": "7e92e99b7fa176d1",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "[{'name': '张三', 'source': '同事', 'tel': '13511112222'},\n {'name': '李四', 'source': '朋友', 'tel': '13900001111'},\n {'name': '王武', 'source': '同事', 'tel': '18912344321'}]"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看通讯录中全部好友\n",
    "db.all()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-23T02:55:02.956338Z",
     "start_time": "2024-02-23T02:55:02.948130Z"
    }
   },
   "id": "4dc43a82e187a279",
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'name': '张三', 'source': '同事', 'tel': '13511112222'}]\n"
     ]
    }
   ],
   "source": [
    "# 根据姓名查看电话\n",
    "from tinydb import Query\n",
    "friend = Query()\n",
    "friend_info = db.search(friend.name == \"张三\")\n",
    "print(friend_info)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-23T02:57:25.189718Z",
     "start_time": "2024-02-23T02:57:25.185655Z"
    }
   },
   "id": "ce85645203d25d43",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三的电话是: 13511112222\n"
     ]
    },
    {
     "data": {
      "text/plain": "'\\nTinyDB是纯Python实现的数据库\\n他还能支持更新、删除、复杂查询等操作\\n更多功能请参考官方文档\\nhttps://pypi.org/project/tinydb/\\n'"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(f\"{friend_info[0]['name']}的电话是: {friend_info[0]['tel']}\")\n",
    "\"\"\"\n",
    "TinyDB是纯Python实现的数据库\n",
    "他还能支持更新、删除、复杂查询等操作\n",
    "更多功能请参考官方文档\n",
    "https://pypi.org/project/tinydb/\n",
    "\"\"\""
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-23T08:17:08.422805Z",
     "start_time": "2024-02-23T08:17:08.414996Z"
    }
   },
   "id": "cae898ba63f771a1",
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on class list in module builtins:\n",
      "\n",
      "class list(object)\n",
      " |  list(iterable=(), /)\n",
      " |  \n",
      " |  Built-in mutable sequence.\n",
      " |  \n",
      " |  If no argument is given, the constructor creates a new empty list.\n",
      " |  The argument must be an iterable if specified.\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __add__(self, value, /)\n",
      " |      Return self+value.\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      Return key in self.\n",
      " |  \n",
      " |  __delitem__(self, key, /)\n",
      " |      Delete self[key].\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(...)\n",
      " |      x.__getitem__(y) <==> x[y]\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __iadd__(self, value, /)\n",
      " |      Implement self+=value.\n",
      " |  \n",
      " |  __imul__(self, value, /)\n",
      " |      Implement self*=value.\n",
      " |  \n",
      " |  __init__(self, /, *args, **kwargs)\n",
      " |      Initialize self.  See help(type(self)) for accurate signature.\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __mul__(self, value, /)\n",
      " |      Return self*value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  __reversed__(self, /)\n",
      " |      Return a reverse iterator over the list.\n",
      " |  \n",
      " |  __rmul__(self, value, /)\n",
      " |      Return value*self.\n",
      " |  \n",
      " |  __setitem__(self, key, value, /)\n",
      " |      Set self[key] to value.\n",
      " |  \n",
      " |  __sizeof__(self, /)\n",
      " |      Return the size of the list in memory, in bytes.\n",
      " |  \n",
      " |  append(self, object, /)\n",
      " |      Append object to the end of the list.\n",
      " |  \n",
      " |  clear(self, /)\n",
      " |      Remove all items from list.\n",
      " |  \n",
      " |  copy(self, /)\n",
      " |      Return a shallow copy of the list.\n",
      " |  \n",
      " |  count(self, value, /)\n",
      " |      Return number of occurrences of value.\n",
      " |  \n",
      " |  extend(self, iterable, /)\n",
      " |      Extend list by appending elements from the iterable.\n",
      " |  \n",
      " |  index(self, value, start=0, stop=9223372036854775807, /)\n",
      " |      Return first index of value.\n",
      " |      \n",
      " |      Raises ValueError if the value is not present.\n",
      " |  \n",
      " |  insert(self, index, object, /)\n",
      " |      Insert object before index.\n",
      " |  \n",
      " |  pop(self, index=-1, /)\n",
      " |      Remove and return item at index (default last).\n",
      " |      \n",
      " |      Raises IndexError if list is empty or index is out of range.\n",
      " |  \n",
      " |  remove(self, value, /)\n",
      " |      Remove first occurrence of value.\n",
      " |      \n",
      " |      Raises ValueError if the value is not present.\n",
      " |  \n",
      " |  reverse(self, /)\n",
      " |      Reverse *IN PLACE*.\n",
      " |  \n",
      " |  sort(self, /, *, key=None, reverse=False)\n",
      " |      Sort the list in ascending order and return None.\n",
      " |      \n",
      " |      The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n",
      " |      order of two equal elements is maintained).\n",
      " |      \n",
      " |      If a key function is given, apply it once to each list item and sort them,\n",
      " |      ascending or descending, according to their function values.\n",
      " |      \n",
      " |      The reverse flag can be set to sort in descending order.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods defined here:\n",
      " |  \n",
      " |  __class_getitem__(...) from builtins.type\n",
      " |      See PEP 585\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  __hash__ = None\n"
     ]
    }
   ],
   "source": [
    "help(list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-23T08:17:14.148097Z",
     "start_time": "2024-02-23T08:17:14.145027Z"
    }
   },
   "id": "57968b391139895e",
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "7282d5fb569e6b05"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
